define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'finance/royalty/index',
                    add_url: 'finance/royalty/add',
                    edit_url: 'finance/royalty/edit',
                    del_url: 'finance/royalty/del',
                    multi_url: 'finance/royalty/multi',
                    excel_url: 'finance/royalty/import',
                    generate_url: 'finance/royalty/send',
                    generate_tax_url: 'finance/royalty/excel',
                    table: 'royalty',
                }
            });

            var table = $("#table");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='admin.department']", form).addClass("selectpage").data("source", "auth/department/index").data("primaryKey", "id").data("field", "name",'id').data("orderBy", "id desc");

                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                  searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'userid', title: __('User_id')},
                       {field: 'weigh', title: __('Weigh'),sortable:true},
                        {field: 'admin.username', title: __('Admin.username'),operate:false},
                        {field: 'admin.mobile', title: __('Admin.mobile'),operate:false},
                        {field: 'admin.department_text', title: __('所属部门'),operate:false},
                        {field: 'admin.department', title: __('所属部门'), visible: false,formatter: Table.api.formatter.search},
                        {field: 'admin.id_number', title: __('身份证号'),operate:false},
                    
                        
                        

                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        import: function () {
            Controller.api.bindevent();
             Controller.api.week();
             $("input[name='row[import_type]']").on('change', function() {
                    var val = $(this).val();
                    if(val == 2){
                         $(".display_id").slideDown();
                    }else{
                         $(".display_id").slideUp();
                    }
                }); 
        },   
        excel: function () {
             Form.api.bindevent($("form[role=form]"), function(data, ret){
                 document.location.href = ret.url;
            }, function(data, ret){
                console.log('错误');
            });
            Controller.api.week();
        },   
         send: function () {
            Controller.api.bindevent();
             Controller.api.week();
             $("input[name='row[send_type]']").on('change', function() {
                    var val = $(this).val();
                    if(val == 2){
                         $(".display_id2").slideDown();
                         $(".display_id3").slideUp();
                    }else if(val == 3){
                        $(".display_id3").slideDown();
                        $(".display_id2").slideUp();
                    }else{
                         $(".display_id2").slideUp();  
                         $(".display_id3").slideUp();
                    }
                }); 
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
             week: function () {
                 setTimeout(function(){
                    $("#c-year").selectpicker('refresh');
                  },800);
                  var DefaultsTime = $("#DefaultsTime").val();
                 $(document).on("change", "#c-year", function () {
                      setTimeout(function(){
                        $("#c-date").selectpicker('val',DefaultsTime);
                        $("#c-date").selectpicker("refresh");
                        $("#c-date").selectpicker();
                      },800); 
                });
            },
        }
    };
    return Controller;
});